extends Node

'''
	世界控制器
'''
@onready var backgroudMusic = $BackgroundMusic
@onready var startMusic = $StartMusic
@onready var ysort = $".."
@onready var timer = $Timer
@onready var part1 = $"../Path/Part1"
@onready var createTimer = $CreateTimer

@onready var part2LeftPath = $"../Path/Part2/LeftPath"
@onready var part2RightPath = $"../Path/Part2/RightPath"
@onready var util = Util.new()

@onready var part3TopPath = $"../Path/Part3/TopPath"
@onready var part3BottomPath = $"../Path/Part3/BottomPath"

@onready var part4 = $"../Path/Part4/Path2D"
@onready var part5 = $"../Path/Part5/Path2D"
@onready var part6Path1 = $"../Path/Part6/Path1"
@onready var part6Path2 = $"../Path/Part6/Path2"
@onready var part7Path1 = $"../Path/Part7/Path1"
@onready var part7Path2 = $"../Path/Part7/Path2"
@onready var part8Path1 = $"../Path/Part8/Path1"
@onready var part8Path2 = $"../Path/Part8/Path2"
@onready var part9Path1 = $"../Path/Part9/Path1"
@onready var part9Path2 = $"../Path/Part9/Path2"
@onready var part9Path3 = $"../Path/Part9/Path3"
@onready var part10Path1 = $"../Path/Part10/Path1"
@onready var part10Path2 = $"../Path/Part10/Path2"

var offset = Vector2(108, 47)

var stateThread = initArray(20, true)
var pathCounts = initArray(20, 0)
var times = initArray(20, 0)
var isInit = initArray(20, true)


var follows = [[], []]
'''
	定时器周期
'''
func _on_timer_timeout():
	if stateThread[0]:
		part1Func()
	
	if !stateThread[0] && stateThread[1]:
		if isInit[1]:
			initEnemiesFollowPath(5, 20, [part2LeftPath, part2RightPath])
			part2LeftPath.visible = true
			part2RightPath.visible = true
			isInit[1] = false
			
		enemiesFollowPathMove(1.5, [part2LeftPath, part2RightPath], 1)
	
	elif !stateThread[1] && stateThread[2]:
		if isInit[2]:
			initEnemiesFollowPath(5, 20, [part3TopPath, part3BottomPath])
			part3TopPath.visible = true
			part3BottomPath.visible = true
			isInit[2] = false
			
		enemiesFollowPathMove(1.5, [part3TopPath, part3BottomPath], 2)
	
	elif !stateThread[2] && stateThread[3]:
		if isInit[3]:
			initEnemiesFollowPath(5, 20, [part2LeftPath, part2RightPath, part3TopPath, part3BottomPath])
			part2LeftPath.visible = true
			part2RightPath.visible = true
			part3TopPath.visible = true
			part3BottomPath.visible = true
			isInit[3] = false
		enemiesFollowPathMove(1.5, [part2LeftPath, part2RightPath, part3TopPath, part3BottomPath], 3)
	
	elif !stateThread[3] && stateThread[4]:
		if isInit[4]:
			initEnemiesFollowPath(10, 20, [part4])
			part4.visible = true
			isInit[4] = false
			
		enemiesFollowPathMove(3, [part4], 4)
		
	elif !stateThread[4] && stateThread[5]:
		if isInit[5]:
			initEnemiesFollowPath(10, 20, [part5])
			part5.visible = true
			isInit[5] = false
			
		enemiesFollowPathMove(2, [part5], 5)
		
	elif !stateThread[5] && stateThread[6]:
		if isInit[6]:
			follows = [[], []]
			part6Path1.visible = true
			part6Path2.visible = true
			isInit[6] = false
			
		spawn_timer -= 0.1
		if spawn_timer <= 0:
			dynamicsGenerateEnemy(follows, [part6Path1, part6Path2])
			spawn_timer = 5
		moveLoop(1.5, 20, 6)
		
	elif !stateThread[6] && stateThread[7]:
		if isInit[7]:
			follows = [[], []]
			part7Path1.visible = true
			part7Path2.visible = true
			isInit[7] = false
			
		spawn_timer -= 0.1
		if spawn_timer <= 0:
			dynamicsGenerateEnemy(follows, [part7Path1, part7Path2])
			spawn_timer = 5
		moveLoop(1.5, 20, 7)
	elif !stateThread[7] && stateThread[8]:
		if isInit[8]:
			follows = [[], []]
			part7Path1.queue_free()
			part7Path2.queue_free()
			part6Path1.queue_free()
			part6Path2.queue_free()
			isInit[8] = false
			
		spawn_timer -= 0.1
		if spawn_timer <= 0:
			dynamicsGenerateEnemy(follows, [part8Path1, part8Path2])
			spawn_timer = 5
		moveLoop(2, 30, 8)
		
	elif !stateThread[8] && stateThread[9]:
		if isInit[9]:
			follows = [[], [], []]
			part8Path1.queue_free()
			part8Path2.queue_free()
			isInit[9] = false
			
		spawn_timer -= 0.1
		if spawn_timer <= 0:
			dynamicsGenerateEnemy(follows, [part9Path1, part9Path2, part9Path3])
			spawn_timer = 5
		moveLoop(3, 50, 9)
		
	elif !stateThread[8] && stateThread[10]:
		if isInit[10]:
			follows = [[], []]
			part9Path1.queue_free()
			part9Path2.queue_free()
			part9Path3.queue_free()
			isInit[10] = false
			
		spawn_timer -= 0.1
		if spawn_timer <= 0:
			dynamicsGenerateEnemy(follows, [part10Path1, part10Path2])
			spawn_timer = 5
		moveLoop(3, 50, 10)
	
'''
	动态生成敌人
	@param follows 跟随组
	@param pathArray 路径组
'''
func dynamicsGenerateEnemy(follows, pathArray):
	for i in range(pathArray.size()):
		var temp = PathFollow2D.new()
		temp.add_child(load("res://Enemy/enemy.tscn").instantiate())
		follows[i].append(temp)
		pathArray[i].add_child(temp)
	

var spawn_timer = 2
func moveLoop(speed, maxNum, runSequency):
	if follows != null:
		for i in range(0, follows.size()):
			for j in range(0, follows[i].size()):
				follows[i][j].progress += speed
				if follows[i].size() >= maxNum:
					stateThread[runSequency] = false
					follows.clear()
					follows = null
					return

func initArray(size, defaultValue):
	var array = []
	for i in size:
		array.append(defaultValue)
		
	return array
	
func _ready():
	timer.start()
	startMusic.play()

'''
	初始化多个敌人的轨道
	@param enemyNum 敌人数量
	@param length 敌人间距
	@param pathArray 轨道组
	@return 路径跟随节点组
'''
func initEnemiesFollowPath(enemyNum, length, pathArray):
	for path in pathArray:
		path.visible = false
	
	var paths = []
	for path in pathArray:
		var follows = []
		for i in enemyNum:
			# 轨道跟随对象
			var follow = PathFollow2D.new()
			follow.add_child(load("res://Enemy/enemy.tscn").instantiate())
			follow.progress = i * length
			path.add_child(follow)
			follows.append(follow)
		paths.append(follows)
	return paths


'''
	让多个轨道上的敌人开始移动
	@param speed 移动速度
	@param pathArray 轨道组
	@param runSequence 运行序列
	@param autoFree 自动销毁开关, 默认自动销毁
'''
func enemiesFollowPathMove(speed, pathArray, runSequence):
	for path in pathArray:
		var children = path.get_children()
		for child in children:
			child.progress += speed
			if child.progress_ratio >= 0.95:
				child.queue_free()
				if children.size() <= 1:
					stateThread[runSequence] = false
'''
	开始音乐播放完毕
'''
func _on_start_music_finished():
	startMusic.stop()
	backgroudMusic.play()


'''
	背景音乐播放完毕
'''
func _on_background_music_finished():
	backgroudMusic.stop()
	
	
func part1Func():
	times[0] += 1
	if times[0] == 8:
		timer.wait_time = 0.01
		
	if times[0] < 9:
		var pathFollow = PathFollow2D.new()
		pathFollow.progress = pathCounts[0]
		pathFollow.add_child(load("res://Enemy/enemy.tscn").instantiate())
		pathCounts[0] += 50
		part1.add_child(pathFollow)
	else:
		if part1 != null:
			part1.queue_free()
			stateThread[0] = false
